package cc.mrbird.febs.customer.controller;


import cc.mrbird.febs.common.annotation.ControllerEndpoint;
import cc.mrbird.febs.common.controller.BaseController;
import cc.mrbird.febs.common.entity.FebsResponse;
import cc.mrbird.febs.common.entity.QueryRequest;
import cc.mrbird.febs.common.utils.FebsUtil;
import cc.mrbird.febs.customer.entity.Customer;
import cc.mrbird.febs.customer.entity.Income;
import cc.mrbird.febs.customer.entity.Order;
import cc.mrbird.febs.customer.service.IActivityService;
import cc.mrbird.febs.customer.service.ICustomerService;
import cc.mrbird.febs.customer.service.IIncomeService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 收入明细表 前端控制器
 * </p>
 *
 * @author Macw
 * @since 2020-11-04
 */
@RestController
@RequestMapping("/income")
public class IncomeController extends BaseController {

    @Resource
    private IIncomeService iIncomeService;

    @Resource
    private ICustomerService customerService;

    @Resource
    private IActivityService activityService;

    @RequestMapping("getIncomeList")
    @RequiresPermissions("income:view")
    @ControllerEndpoint(operation = "查询收入详情", exceptionMessage = "查询收入详情失败")
    public FebsResponse getIncomeList(Income income, QueryRequest request){

//        IPage<Income> page1 = iIncomeService.selectIncomeByCondition(income,request);
        Page<Income> page = new Page<>(request.getPageNum(), request.getPageSize());
        IPage<Income> page2 = iIncomeService.page(page, new QueryWrapper<Income>().lambda()
                .eq(Income::getPid, 0)
                .eq(Income::getCustomerId,income.getCustomerId())
                .like(StringUtils.isNotBlank(income.getActivityName()), Income::getActivityName, income.getActivityName())
                .or()
                .eq(Income::getPid,income.getCustomerId())
                .orderByDesc(Income::getCreateTime));
        List<Income> records = page2.getRecords();
        records.forEach(s->{
            s.setNickName(customerService.getById(s.getCustomerId()).getNickname());
            s.setHeadImgUrl(customerService.getById(s.getCustomerId()).getHeadimgurl());
        });
//        Page<Customer> page = new Page<>(request.getPageNum(), request.getPageSize());
//        IPage<Customer> page1 = customerService.page(page, new QueryWrapper<Customer>()
//                .lambda()
//                .eq(Customer::getPid, income.getCustomerId()));
//        List<Customer> records = page1.getRecords();
//        records.forEach(s->{
//
//        });
        return new FebsResponse().success().data(getDataTable(page2));
    }

//    @RequestMapping("updateIncome")
//    @RequiresPermissions("income:update")
//    public FebsResponse updateIncome(Income income){
//        Income byId = iIncomeService.getById(income.getIncomeId());
//        //未提现的
//        Double leavePrice = byId.getLeavePrice().doubleValue();
//        //本次提现的数目
//        Double withdrawPrice = income.getWithdrawPrice().doubleValue();
//        //已提现金额
//        double receivePrice = byId.getReceivePrice().doubleValue();
//        if (withdrawPrice> leavePrice){
//            return new FebsResponse().fail().message("余额不足");
//        }
//        if (leavePrice - withdrawPrice==0){
//            receivePrice += withdrawPrice;
//            income.setStatus(1);
//            income.setLeavePrice(BigDecimal.valueOf(0.0));
//            income.setReceivePrice(BigDecimal.valueOf(receivePrice));
//        }else if (leavePrice - withdrawPrice>0){
//            leavePrice = leavePrice - withdrawPrice;
//            receivePrice += withdrawPrice;
//            income.setLeavePrice(BigDecimal.valueOf(leavePrice));
//            income.setReceivePrice(BigDecimal.valueOf(receivePrice));
//            income.setStatus(2);
//        }
//        iIncomeService.updateById(income);
//        return new FebsResponse().success();
//    }

}
